home *** CD-ROM | disk | FTP | other *** search
/ Express Pd: GALORE / Express Pd Galore - The Amiga PD & Shareware CD (1994)(Express Pd)[!][Amiga-CD32-CDTV].iso / productivity / term / termsendtext.c < prev    next >
C/C++ Source or Header  |  1993-07-16  |  8KB  |  529 lines

  1. /*
  2. **    termSendText.c
  3. **
  4. **    Text sending support routines
  5. **
  6. **    Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termARexxGlobal.h"
  11.  
  12.     /* Local variables. */
  13.  
  14. STATIC LONG WaitCount,PromptCount;
  15.  
  16.     /* MatchPrompt():
  17.      *
  18.      *    Search incoming data stream for a match.
  19.      */
  20.  
  21. STATIC BYTE __regargs
  22. MatchPrompt(register STRPTR Data,register LONG Size,register STRPTR Prompt,register LONG PromptLen)
  23. {
  24.     register UBYTE c,Mask;
  25.  
  26.     if(Config -> SerialConfig -> StripBit8)
  27.         Mask = 0x7F;
  28.     else
  29.         Mask = 0xFF;
  30.  
  31.     do
  32.     {
  33.         if(c = ((*Data++) & Mask))
  34.         {
  35.             register BYTE MatchMade;
  36.  
  37.             do
  38.             {
  39.                 MatchMade = FALSE;
  40.  
  41.                 if(PromptCount == WaitCount)
  42.                 {
  43.                     if(c == Prompt[WaitCount] & Mask)
  44.                     {
  45.                         MatchMade = TRUE;
  46.  
  47.                         if(PromptLen == ++PromptCount)
  48.                             return(TRUE);
  49.                     }
  50.                 }
  51.  
  52.                 if(MatchMade)
  53.                     WaitCount++;
  54.                 else
  55.                 {
  56.                     if(WaitCount)
  57.                     {
  58.                         WaitCount = 0;
  59.  
  60.                         PromptCount = 0;
  61.                     }
  62.                     else
  63.                         break;
  64.                 }
  65.             }
  66.             while(!WaitCount);
  67.         }
  68.     }
  69.     while(--Size);
  70.  
  71.     return(FALSE);
  72. }
  73.  
  74.     /* WaitForPrompt(STRPTR Prompt,LONG PromptLen):
  75.      *
  76.      *    Scan the incoming data flow for a certain string.
  77.      */
  78.  
  79. STATIC BYTE __regargs
  80. WaitForPrompt(STRPTR Prompt,LONG PromptLen)
  81. {
  82.     ULONG Signals;
  83.  
  84.     WaitCount = PromptCount = 0;
  85.  
  86.     if(DataHold)
  87.     {
  88.         DataHold = NULL;
  89.  
  90.         RestartSerial();
  91.     }
  92.  
  93.     if(CheckIO(ReadRequest))
  94.         Signals = SIG_SERIAL;
  95.     else
  96.         Signals = NULL;
  97.  
  98.     StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
  99.  
  100.     for(;;)
  101.     {
  102.         if(Signals & SIG_SERIAL)
  103.         {
  104.             if(!WaitIO(ReadRequest))
  105.             {
  106.                 LONG Length;
  107.  
  108.                 BytesIn++;
  109.  
  110.                 ConProcess(ReadBuffer,1);
  111.  
  112.                 Status = STATUS_READY;
  113.  
  114.                 if(MatchPrompt(ReadBuffer,1,Prompt,PromptLen))
  115.                 {
  116.                     if(!CheckIO(TimeRequest))
  117.                         AbortIO(TimeRequest);
  118.  
  119.                     WaitIO(TimeRequest);
  120.  
  121.                     RestartSerial();
  122.  
  123.                     return(TRUE);
  124.                 }
  125.  
  126.                 do
  127.                 {
  128.                         /* Check how many bytes are still in
  129.                          * the serial buffer.
  130.                          */
  131.  
  132.                     WriteRequest -> IOSer . io_Command = SDCMD_QUERY;
  133.  
  134.                     DoIO(WriteRequest);
  135.  
  136.                     if(Length = WriteRequest -> IOSer . io_Actual)
  137.                     {
  138.                         if(Length > Config -> SerialConfig -> SerialBufferSize)
  139.                             Length = Config -> SerialConfig -> SerialBufferSize;
  140.  
  141.                         ReadRequest -> IOSer . io_Command    = CMD_READ;
  142.                         ReadRequest -> IOSer . io_Data        = ReadBuffer;
  143.                         ReadRequest -> IOSer . io_Length    = Length;
  144.  
  145.                         if(!DoIO(ReadRequest))
  146.                         {
  147.                             BytesIn += Length;
  148.  
  149.                             ConProcess(ReadBuffer,Length);
  150.  
  151.                             Status = STATUS_READY;
  152.  
  153.                             if(MatchPrompt(ReadBuffer,Length,Prompt,PromptLen))
  154.                             {
  155.                                 if(!CheckIO(TimeRequest))
  156.                                     AbortIO(TimeRequest);
  157.  
  158.                                 WaitIO(TimeRequest);
  159.  
  160.                                 RestartSerial();
  161.  
  162.                                 return(TRUE);
  163.                             }
  164.                         }
  165.                     }
  166.                 }
  167.                 while(Length);
  168.             }
  169.  
  170.             RestartSerial();
  171.         }
  172.  
  173.         if(Signals & SIG_TIMER)
  174.         {
  175.             WaitIO(TimeRequest);
  176.  
  177.             return(FALSE);
  178.         }
  179.  
  180.         Signals = Wait(SIG_SERIAL | SIG_TIMER);
  181.     }
  182. }
  183.  
  184.     /* SendLinePrompt(STRPTR Line,LONG Len):
  185.      *
  186.      *    Send text line, wait for prompt.
  187.      */
  188.  
  189. BYTE __regargs
  190. SendLinePrompt(STRPTR Line,LONG Len)
  191. {
  192.     LONG i;
  193.  
  194.     if(Len == -1)
  195.         Len = strlen(Line);
  196.  
  197.     while(Len)
  198.     {
  199.         i = 0;
  200.  
  201.         while(i < Len && Line[i] != '\r')
  202.             i++;
  203.  
  204.  
  205.         if(Line[i] == '\r')
  206.         {
  207.             i++;
  208.  
  209.             SerWrite(Line,i);
  210.  
  211.             if(!WaitForPrompt(SendPrompt,SendPromptLen))
  212.                 return(FALSE);
  213.         }
  214.         else
  215.         {
  216.             if(i)
  217.                 SerWrite(Line,i);
  218.         }
  219.  
  220.         Len    -= i;
  221.         Line    += i;
  222.     }
  223.  
  224.     return(TRUE);
  225. }
  226.  
  227.     /* SendLineSimple(STRPTR Line,LONG Len):
  228.      *
  229.      *    Send a text line, no fancy features.
  230.      */
  231.  
  232. BYTE __regargs
  233. SendLineSimple(STRPTR Line,LONG Len)
  234. {
  235.     if(Len == -1)
  236.         Len = strlen(Line);
  237.  
  238.     SerWrite(Line,Len);
  239.  
  240.     return(TRUE);
  241. }
  242.  
  243.     /* SendLineDelay(STRPTR Line,LONG Len):
  244.      *
  245.      *    Send a text line, include a delay where necessary.
  246.      */
  247.  
  248. BYTE __regargs
  249. SendLineDelay(STRPTR Line,LONG Len)
  250. {
  251.     if(Len == -1)
  252.         Len = strlen(Line);
  253.  
  254.     while(Len--)
  255.     {
  256.         SerWrite(Line,1);
  257.  
  258.         if(*Line == '\r')
  259.         {
  260.             if(Config -> ClipConfig -> LineDelay)
  261.                 WaitTime(Config -> ClipConfig -> LineDelay / 100,(Config -> ClipConfig -> LineDelay % 100) * 10000);
  262.         }
  263.         else
  264.         {
  265.             if(Config -> ClipConfig -> CharDelay)
  266.                 WaitTime(Config -> ClipConfig -> CharDelay / 100,(Config -> ClipConfig -> CharDelay % 100) * 10000);
  267.         }
  268.  
  269.         Line++;
  270.     }
  271.  
  272.     return(TRUE);
  273. }
  274.  
  275.     /* SendLineEcho(STRPTR Line,LONG Len):
  276.      *
  277.      *    Send a text line, wait for single characters to be echoed.
  278.      */
  279.  
  280. BYTE __regargs
  281. SendLineEcho(STRPTR Line,LONG Len)
  282. {
  283.     ULONG Signals;
  284.  
  285.     if(DataHold)
  286.     {
  287.         DataHold = NULL;
  288.  
  289.         RestartSerial();
  290.     }
  291.  
  292.     if(Len == -1)
  293.         Len = strlen(Line);
  294.  
  295.     while(Len--)
  296.     {
  297.         SerWrite(Line,1);
  298.  
  299.         StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
  300.  
  301.         FOREVER
  302.         {
  303.             Signals = Wait(SIG_TIMER | SIG_SERIAL);
  304.  
  305.             if(Signals & SIG_SERIAL)
  306.             {
  307.                 if(!WaitIO(ReadRequest))
  308.                 {
  309.                     if(*(UBYTE *)ReadBuffer == *Line)
  310.                     {
  311.                         if(!CheckIO(TimeRequest))
  312.                             AbortIO(TimeRequest);
  313.  
  314.                         WaitIO(TimeRequest);
  315.  
  316.                         RestartSerial();
  317.  
  318.                         break;
  319.                     }
  320.  
  321.                     RestartSerial();
  322.                 }
  323.                 else
  324.                 {
  325.                     if(!CheckIO(TimeRequest))
  326.                         AbortIO(TimeRequest);
  327.  
  328.                     WaitIO(TimeRequest);
  329.  
  330.                     RestartSerial();
  331.  
  332.                     return(FALSE);
  333.                 }
  334.             }
  335.  
  336.             if(Signals & SIG_TIMER)
  337.             {
  338.                 WaitIO(TimeRequest);
  339.  
  340.                 return(FALSE);
  341.             }
  342.         }
  343.  
  344.         Line++;
  345.     }
  346.  
  347.     return(TRUE);
  348. }
  349.  
  350.     /* SendLineAnyEcho(STRPTR Line,LONG Len):
  351.      *
  352.      *    Send a text line, wait for characters to be echoed.
  353.      */
  354.  
  355. BYTE __regargs
  356. SendLineAnyEcho(STRPTR Line,LONG Len)
  357. {
  358.     ULONG Signals;
  359.  
  360.     if(DataHold)
  361.     {
  362.         DataHold = NULL;
  363.  
  364.         RestartSerial();
  365.     }
  366.  
  367.     if(Len == -1)
  368.         Len = strlen(Line);
  369.  
  370.     while(Len--)
  371.     {
  372.         SerWrite(Line,1);
  373.  
  374.         StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
  375.  
  376.         FOREVER
  377.         {
  378.             Signals = Wait(SIG_TIMER | SIG_SERIAL);
  379.  
  380.             if(Signals & SIG_SERIAL)
  381.             {
  382.                 if(!WaitIO(ReadRequest))
  383.                 {
  384.                     if(!CheckIO(TimeRequest))
  385.                         AbortIO(TimeRequest);
  386.  
  387.                     WaitIO(TimeRequest);
  388.  
  389.                     RestartSerial();
  390.  
  391.                     break;
  392.                 }
  393.                 else
  394.                 {
  395.                     if(!CheckIO(TimeRequest))
  396.                         AbortIO(TimeRequest);
  397.  
  398.                     WaitIO(TimeRequest);
  399.  
  400.                     RestartSerial();
  401.  
  402.                     return(FALSE);
  403.                 }
  404.             }
  405.  
  406.             if(Signals & SIG_TIMER)
  407.             {
  408.                 WaitIO(TimeRequest);
  409.  
  410.                 return(FALSE);
  411.             }
  412.         }
  413.  
  414.         Line++;
  415.     }
  416.  
  417.     return(TRUE);
  418. }
  419.  
  420.     /* SendLineKeyDelay(STRPTR Line,LONG Len):
  421.      *
  422.      *    Send a text line, include keyboard delay pauses between characters.
  423.      */
  424.  
  425. BYTE __regargs
  426. SendLineKeyDelay(STRPTR Line,LONG Len)
  427. {
  428.     struct Preferences Prefs;
  429.  
  430.     if(Len == -1)
  431.         Len = strlen(Line);
  432.  
  433.         /* Get current key repeat delay. */
  434.  
  435.     GetPrefs(&Prefs,offsetof(struct Preferences,KeyRptDelay));
  436.  
  437.         /* Any delay specified at all? */
  438.  
  439.     if(Prefs . KeyRptSpeed . tv_secs || Prefs . KeyRptSpeed . tv_micro)
  440.     {
  441.         while(Len--)
  442.         {
  443.             SerWrite(Line++,1);
  444.  
  445.             if(Len)
  446.             {
  447.                 TimeRequest -> tr_node . io_Command    = TR_ADDREQUEST;
  448.                 TimeRequest -> tr_time            = Prefs . KeyRptSpeed;
  449.  
  450.                 DoIO(TimeRequest);
  451.             }
  452.         }
  453.     }
  454.     else
  455.         SerWrite(Line,Len);
  456.  
  457.     return(TRUE);
  458. }
  459.  
  460.     /* SendSetup():
  461.      *
  462.      *    Choose the right routine for the text line output job.
  463.      */
  464.  
  465. VOID
  466. SendSetup()
  467. {
  468.         /* Prepare the prompt string. */
  469.  
  470.     if(Config -> ClipConfig -> LinePrompt[0])
  471.         SendPromptLen = TranslateString(Config -> ClipConfig -> LinePrompt,SendPrompt);
  472.     else
  473.     {
  474.         SendPrompt[0] = 0;
  475.         SendPromptLen = 0;
  476.     }
  477.  
  478.         /* Pick the line send routine. */
  479.  
  480.     switch(Config -> ClipConfig -> PacingMode)
  481.     {
  482.         case PACE_DIRECT:
  483.  
  484.             SendLine = SendLineSimple;
  485.             break;
  486.  
  487.         case PACE_ECHO:
  488.  
  489.             if(Config -> ClipConfig -> SendTimeout)
  490.                 SendLine = SendLineEcho;
  491.             else
  492.                 SendLine = SendLineSimple;
  493.  
  494.             break;
  495.  
  496.         case PACE_ANYECHO:
  497.  
  498.             if(Config -> ClipConfig -> SendTimeout)
  499.                 SendLine = SendLineAnyEcho;
  500.             else
  501.                 SendLine = SendLineSimple;
  502.  
  503.             break;
  504.  
  505.         case PACE_PROMPT:
  506.  
  507.             if(Config -> ClipConfig -> SendTimeout)
  508.                 SendLine = SendLinePrompt;
  509.             else
  510.                 SendLine = SendLineSimple;
  511.  
  512.             break;
  513.  
  514.         case PACE_DELAY:
  515.  
  516.             if(Config -> ClipConfig -> LineDelay || Config -> ClipConfig -> CharDelay)
  517.                 SendLine = SendLineDelay;
  518.             else
  519.                 SendLine = SendLineSimple;
  520.  
  521.             break;
  522.  
  523.         case PACE_KEYBOARD:
  524.  
  525.             SendLine = SendLineKeyDelay;
  526.             break;
  527.     }
  528. }
  529.